package com.artpri.action.user;

import java.util.List;

import com.artpri.action.BaseAction;
import com.artpri.model.GoodsOrder;
import com.artpri.model.OrderInfo;
import com.artpri.model.UserInfo;
import com.artpri.model.vo.OrderInfoVo;
import com.artpri.service.OrderInfoService.OrderType;

/**
 * 订单 
 * @author ZhangSi
 */
public class OrderInfoAction extends BaseAction {

	private static final long serialVersionUID = 8452771718521977749L;

	//提交订单
	private OrderInfo order;
	private Integer addId;
	private List<GoodsOrder> orderGoods;

	//订单列表
	private List<OrderInfoVo> orderList;
	private String orderType;
	
	//订单取消
	private Integer id;
	private String cancelReason;

	private Integer pageNo;
	private Integer totalRecords;
	private Integer pageSize;
	
	//消费记录
	private List<OrderInfo> consumeList;
	
	private UserInfo user = getSessionUser();
	
	/**
	 * 提交订单
	 */
	public String referOrder() throws Exception {
		order.setUserNum(user.getUserNum());
		getOrderInfoService().addOrder(order, orderGoods, addId);
		return SUCCESS;
	}

	/**
	 * 订单列表
	 */
	public String listOrder() throws Exception {
		totalRecords = getOrderInfoService().getOrderCount(user.getUserNum(), orderType);
		orderList = getOrderInfoService().listOrderVoByPage(user.getUserNum(), orderType, getPageNo(), pageSize);
		return orderType;
	}
	
	/**
	 * 取消订单
	 */
	public String cancelOrder() throws Exception {
		getOrderInfoService().cancelOrder(id, cancelReason);
		return SUCCESS;
	}
	
	/**
	 * 取消订单记录
	 */
	public String cancelOrderList() throws Exception {
		orderList = getOrderInfoService().listOrderVoByPage(user.getUserNum(), OrderType.CANCELED, getPageNo(), pageSize);
		totalRecords = getOrderInfoService().getOrderCount(user.getUserNum(), OrderType.CANCELED);
		return SUCCESS;
	}
	
	/**
	 * 消费记录
	 */
	public String consumeCalendar() throws Exception {
		consumeList = getOrderInfoService().listOrderByPage(user.getUserNum(), OrderType.DONE_DEAL, getPageNo(), getPageSize());
		totalRecords = getOrderInfoService().getOrderCount(user.getUserNum(), OrderType.DONE_DEAL);
		System.out.println("totalRecords: " + totalRecords);
		return SUCCESS;
	}

	public OrderInfo getOrder() {
		return order;
	}

	public void setOrder(OrderInfo order) {
		this.order = order;
	}
	
	public Integer getAddId() {
		return addId;
	}

	public void setAddId(Integer addId) {
		this.addId = addId;
	}

	public List<OrderInfoVo> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<OrderInfoVo> orderList) {
		this.orderList = orderList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCancelReason() {
		return cancelReason;
	}

	public void setCancelReason(String cancelReason) {
		this.cancelReason = cancelReason;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	public List<GoodsOrder> getOrderGoods() {
		return orderGoods;
	}

	public void setOrderGoods(List<GoodsOrder> orderGoods) {
		this.orderGoods = orderGoods;
	}

	public Integer getPageNo() {
		pageNo = pageNo == null ? 1 : pageNo;
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getTotalPage() {
		return totalRecords%pageSize==0 ? totalRecords/pageSize : totalRecords/pageSize +1;
	}

	public Integer getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(Integer totalRecords) {
		this.totalRecords = totalRecords;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public List<OrderInfo> getConsumeList() {
		return consumeList;
	}

	public void setConsumeList(List<OrderInfo> consumeList) {
		this.consumeList = consumeList;
	}
}
